package org.taobao.ibatiside.ui.action;

import org.eclipse.core.resources.IFile;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.ide.IDE;
import org.taobao.ibatiside.core.filemodel.IbatisConfigFile;
import org.taobao.ibatiside.ui.Activator;
import org.taobao.ibatiside.ui.wizard.OverViewNewMapperWizard;

public class OverviewNewMapperAction extends OverviewAction {
	public static final String ID="org.taobao.ibatiside.ui.action.iov.newmapper";
	public OverviewNewMapperAction() {
		super();
		setId(ID);
		setText("New Mapper");
		setImageDescriptor(Activator.getIcon("obj16/ibatismap.png"));
	}
	@Override
	public void run() {
		IbatisConfigFile config = (IbatisConfigFile) getSelectObject();
		OverViewNewMapperWizard wizard = new OverViewNewMapperWizard(config.getConfigFile());
		wizard.setWindowTitle("Create new sqlmap for configuration "+config.getConfigFile().getName());
		WizardDialog dialog = new WizardDialog(viewer.getControl().getShell(), wizard);
		if (dialog.open() == WizardDialog.OK){
			viewer.refresh(config);
		}
	}
	
	private void openFile(IFile file) {
		IWorkbenchWindow dw = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
		try {
			if (dw != null) {
				IWorkbenchPage page = dw.getActivePage();
				if (page != null) {
					IDE.openEditor(page, file, true);
				}
			}
		} catch (PartInitException e) {
			e.printStackTrace();
		}

	}

}
